package slr.comment.portlet.ui;

import java.util.List;

import slr.comment.model.SlrAnchor;
import slr.comment.model.SlrDocument;
import slr.comment.model.SlrDocumentStructure;
import slr.comment.model.SlrDocumentTyp;
import slr.comment.portlet.data.SlrExplorerContainer;
import slr.comment.service.SlrAnchorLocalServiceUtil;
import slr.comment.service.SlrDocumentLocalServiceUtil;
import slr.comment.service.SlrDocumentStructureLocalServiceUtil;
import slr.comment.service.SlrDocumentTypLocalServiceUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Validator;
import com.vaadin.data.Item;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.Tree;
/**
 * The left hand side of the explorer Containing the Hierarchial Tree are displayed.
 * @author TransIT mPower Labs
 *
 */
@SuppressWarnings("serial")
public class SlrExplorerTree extends Tree {

	public final static String SLR_DOC_TYP_PROPERTY_NAME = "slrDocumentTypName";
	public final static String SLR_DOC_PROPERTY_NAME = "slrDocumentName";
	public final static String SLR_ANCHOR_PROPERTY_NAME = "slrAnchorName";

	public SlrExplorerTree(final SplitPanel splitPanel) {
		setContainerDataSource(getContainerData());
		final SlrExplorerContainer SlrExplorerContainer = new SlrExplorerContainer();
		addListener(new ItemClickListener() {
			public void itemClick(ItemClickEvent event) {
				Tree tree = (Tree) event.getSource();
				tree.select(event.getItemId());
				tree.expandItem(event.getItemId());
				SlrDocumentTyp slrDocumentTyp = (SlrDocumentTyp) event.getItem()
						.getItemProperty(SLR_DOC_TYP_PROPERTY_NAME).getValue();
				SlrDocument slrDocument = (SlrDocument) event.getItem().getItemProperty(SLR_DOC_PROPERTY_NAME)
						.getValue();
				SlrAnchor slrAnchor = (SlrAnchor) event.getItem().getItemProperty(SLR_ANCHOR_PROPERTY_NAME).getValue();
				if (Validator.isNotNull(slrDocumentTyp)) {
					splitPanel.setSecondComponent(SlrExplorerContainer.addTabSheets(slrDocumentTyp));
				} else if (Validator.isNotNull(slrDocument)) {
					splitPanel.setSecondComponent(SlrExplorerContainer.addTabSheets(slrDocument));
				} else if (Validator.isNotNull(slrAnchor)) {
					splitPanel.setSecondComponent(SlrExplorerContainer.addTabSheets(slrAnchor));
				}
			}
		});

		setImmediate(true);
		setSelectable(true);
		setHeight(100, UNITS_PERCENTAGE);
		setNullSelectionAllowed(false);

		for (Object id : rootItemIds()) {
			collapseItemsRecursively(id);
		}
	}
/**
 * gets the HierarchicalContainer Tree with Nodes of the tree being added up.
 * @return HierarchicalContainer
 */
	public HierarchicalContainer getContainerData() {
		Item item = null;
		String itemId = "";
		String itemId_doc = "";
		String itemId_str = "";
		int docTypListCount;
		List<SlrDocumentTyp> slrDocumentTypList = null;
		List<SlrDocument> slrDocumentList = null;
		List<SlrDocumentStructure> slrDocumentStructureList = null;
		SlrDocumentTyp slrDocumentTyp = null;
		SlrDocument slrDocument = null;
		SlrDocumentStructure slrDocumentStructure = null;
		SlrAnchor slrAnchor = null;

		try {
			docTypListCount = SlrDocumentTypLocalServiceUtil.getSlrDocumentTypsCount();
			slrDocumentTypList = SlrDocumentTypLocalServiceUtil.getSlrDocumentTyps(0, docTypListCount);
		} catch (SystemException e) {
			_log.error(e.getMessage());
		}

		HierarchicalContainer hContainer = new HierarchicalContainer();
		hContainer.addContainerProperty(SLR_DOC_TYP_PROPERTY_NAME, SlrDocumentTyp.class, null);
		hContainer.addContainerProperty(SLR_DOC_PROPERTY_NAME, SlrDocument.class, null);
		hContainer.addContainerProperty(SLR_ANCHOR_PROPERTY_NAME, SlrAnchor.class, null);

		if (Validator.isNotNull(slrDocumentTypList) && slrDocumentTypList.size() != 0) {
			for (int i = 0; i < slrDocumentTypList.size(); i++) {
				slrDocumentTyp = (SlrDocumentTyp) slrDocumentTypList.get(i);
				itemId = slrDocumentTyp.getSlrDocTypLabel();
				item = hContainer.addItem(itemId);
				item.getItemProperty(SLR_DOC_TYP_PROPERTY_NAME).setValue(slrDocumentTyp);
				hContainer.setChildrenAllowed(itemId, true);
				if (Validator.isNotNull(slrDocumentTyp)) {
					try {
						slrDocumentList = SlrDocumentLocalServiceUtil.findByDocumentTypId(slrDocumentTyp
								.getSlrDocumentTypId());
					} catch (SystemException e) {
						_log.error(e.getMessage());
					}
					if (Validator.isNotNull(slrDocumentList) && slrDocumentList.size() > 0) {
						for (int j = 0; j < slrDocumentList.size(); j++) {
							slrDocument = (SlrDocument) slrDocumentList.get(j);
							if (Validator.isNotNull(slrDocument)) {
								itemId_doc = slrDocument.getSlrDocumentPeriodLabel();
								item = hContainer.addItem(itemId_doc);
								item.getItemProperty(SLR_DOC_PROPERTY_NAME).setValue(slrDocument);
								hContainer.setParent(itemId_doc, itemId);
								hContainer.setChildrenAllowed(itemId_doc, true);
								try {
									slrDocumentStructureList = SlrDocumentStructureLocalServiceUtil
											.findByDocumentTypId(slrDocumentTyp.getSlrDocumentTypId());
									if (Validator.isNotNull(slrDocumentStructureList)
											&& slrDocumentStructureList.size() > 0) {
										for (int k = 0; k < slrDocumentStructureList.size(); k++) {
											slrDocumentStructure = (SlrDocumentStructure) slrDocumentStructureList
													.get(k);
											if (Validator.isNotNull(slrDocumentStructure)
													&& slrDocumentStructure.getSlrAnchorId() != 0) {
												try {
													slrAnchor = SlrAnchorLocalServiceUtil
															.getSlrAnchor(slrDocumentStructure.getSlrAnchorId());
												} catch (PortalException e) {
													_log.error(e.getMessage());
												}
												itemId_str = String.valueOf(slrAnchor.getSlrAnchorLabel());
												item = hContainer.addItem(itemId_str);
												item.getItemProperty(SLR_ANCHOR_PROPERTY_NAME).setValue(slrAnchor);
												hContainer.setParent(itemId_str, itemId_doc);
												hContainer.setChildrenAllowed(itemId_str, false);
											}
										}
									}
								} catch (SystemException e) {
									_log.error(e.getMessage());
								}
							}
						}
					}
				}
			}
		}
		return hContainer;
	}
    private static Log _log = LogFactoryUtil.getLog(SlrExplorerTree.class);

}